﻿using CarEdge.Infrastructure.Attr;
using CarEdge.Infrastructure.Dto.Request.login;
using CarEdge.Infrastructure.Dto.Response;
using CarEdge.Infrastructure.EFCore.MySql;
using CarEdge.Infrastructure.IOC;
using CarEdge.Infrastructure.Tools;
using CarEdge.Service.EFCore;
using CarEdge.Service.Interface;
using Crm.Infrastructure.Tools;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CarEdge.Service.Instance
{
    [Inject_]
    public class Login_Service :Base_Service,I_Login_Service
    {
        private readonly Login_IOC _login_IOC;

        public Login_Service(Login_IOC login_IOC)
        {
            _login_IOC = login_IOC;

        }
        /// <summary>
        /// 根据最后登录的code  来获取登录用户的信息
        /// </summary>
        /// <param name="code"></param>
        /// <param name="v"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<SysUser?> Get_User_ByLastCode(string code, string ip)
        {
            var iq = _login_IOC._sys_Login_Log_EFCore.QueryAll(d => d.Llcode == code
           && d.Llip == ip);
            if (!await iq.AnyAsync())
            {
                return null;
            }
            var log = await iq.SingleAsync();
            var last = await _login_IOC._sys_Login_Log_EFCore.QueryAll(false, o => o.LlcreateTime, d => d.Lluid == log.Lluid).FirstAsync();

            if (log.Llid != last.Llid)
            {
                return null;
            }
            return await _login_IOC._sys_User_EFCore.QueryAll(d => d.Uid == log.Lluid).SingleAsync();
        }

        /// <summary>
        /// 管理员登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<Api_Response_Dto> Logins(Login_Request_Dto dto)
        {
            var iq =_login_IOC._sys_User_EFCore.QueryAll(d => d.Uaccount == dto.account);
            if (!await iq.AnyAsync())
            {
                return Result(0, "账号或者密码错误");
            }
            var user=await iq.SingleAsync();//没有数据则会报错

            if (user.UisActive==0)
            {
                return Result(0, "账号未激活");
            }
            if (user.UisBan==1)
            {
                return Result(0, "账号已禁用");
            }
            var pwd=EncryptUtil.LoginMd5(dto.password!, user.Usalt!);
            if (user.Upassword != pwd)
            {
                return Result(0, "账号或者密码错误");
            }
            SysLoginLog sysLoginLog = new SysLoginLog()
            {
                Llid = Config.GUID(),
                Llcode = dto.code,
                LlcreateTime = DateTime.Now,
                Llexplain = null,
                Llip = Config.GetIp(),
                Llresult = 0,
                Lltype = "admin",
                Lluid=user.Uid
            };  
            _login_IOC._sys_Login_Log_EFCore.Add(sysLoginLog);
            _login_IOC._sys_Login_Log_EFCore.SaveChanges();
            return Result(1,"ok");
         
        }
    }
}
